import re

from django.db import models
from django.utils.translation import gettext_lazy as _

CVSS_REGEX = re.compile(r'^CVSS:(2|3\.0|3\.1|4\.0)(/[A-Za-z]+:[A-Za-z]+)+$')


class CVSSLevel(models.TextChoices):
    INFO = 'info', _('Info')
    LOW = 'low', _('Low')
    MEDIUM = 'medium', _('Medium')
    HIGH = 'high', _('High')
    CRITICAL = 'critical', _('Critical')


def level_from_score(score: float) -> CVSSLevel:
    """
    Calculate the CVSS level from a CVSS score.
    """
    if score >= 9.0:
        return CVSSLevel.CRITICAL
    elif score >= 7.0:
        return CVSSLevel.HIGH
    elif score >= 4.0:
        return CVSSLevel.MEDIUM
    elif score > 0:
        return CVSSLevel.LOW
    else:
        return CVSSLevel.INFO


def level_number_from_score(score: float) -> int:
    if score >= 9.0:
        return 5
    elif score >= 7.0:
        return 4
    elif score >= 4.0:
        return 3
    elif score > 0:
        return 2
    else:
        return 1
